home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / macgzip_03b2-src / macos / think / DoOpen.c next >
Encoding:
C/C++ Source or Header  |  1995-01-09  |  8.2 KB  |  431 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <ctype.h>
  9.  
  10. #include <ICAPI.h>
  11. #include <ICKeys.h>
  12. #include "ICMappings.h"
  13.  
  14. #include "MacGzip.h"
  15. #include "ThePrefs.h"
  16. #include "MacErrors.h"
  17.  
  18. #include "tailor.h"
  19.  
  20. #define    GvRefNum    0    /* Default dir */
  21.  
  22. typedef struct {
  23.         OSType    Type;
  24.         OSType    Creator;
  25.         short    Binary;
  26.         short    BinHex;
  27.         char    Suffix[32];
  28. #ifdef    GET_DESC
  29.         char    Description[256]; /* useless for me */
  30. #endif
  31.         Boolean    Found;
  32.         }    TSufMap;
  33.         
  34. TSufMap    SufMap;
  35.  
  36. int            AsciiMode(char *name, int compress);
  37. void        FixMacFile( char* name, int ascii, int decompress);
  38. OSErr        OpenFile( Str255 fName);
  39.  
  40. extern int    gzip_main (Str255 fName, short Compress);
  41.  
  42.  
  43. OSErr OpenFile( Str255 fName)
  44. {
  45.     char            sTemp[256], *c;
  46.  
  47.     Boolean            Compress;
  48.     extern Boolean    modKey;
  49.     
  50.     ParamBlockRec    myParamBlock;
  51.     FInfo            vFInfo;
  52.     extern char        *get_suffix(char *name);
  53.     extern char        z_suffix[MAX_SUFFIX+1];        /* default suffix */
  54.     extern int        z_len;                        /* strlen(z_suffix) */
  55.  
  56.  
  57. /*********** ResFork ***************/
  58.  
  59.     myParamBlock.fileParam.ioCompletion =    NULL;
  60.     myParamBlock.fileParam.ioNamePtr =        fName;
  61.     myParamBlock.fileParam.ioVRefNum =        0;
  62.     myParamBlock.fileParam.ioFRefNum =        0;
  63.     myParamBlock.fileParam.ioFDirIndex =    0 ;
  64.     
  65.     PBGetFInfo( &myParamBlock, false );
  66.     if ( myParamBlock.fileParam.ioFlRLgLen != 0 )
  67.     {
  68.         sprintf(sTemp,"Discard Resource Fork: %#s?",fName);
  69.         if ( Cask( sTemp ) == cancel )
  70.             return(Cancelled);
  71.     }
  72.     
  73. /*    globals in gzip.c */
  74.  
  75.     strncpy(z_suffix, Z_SUFFIX, sizeof(z_suffix)-1);
  76.     if ( *currPrefs.suffix != 0x00 )
  77.     {
  78.         sprintf(z_suffix,"%#s",(char*)currPrefs.suffix);
  79. #ifdef NO_MULTIPLE_DOTS
  80.         if (*z_suffix == '.')
  81.             memmove(z_suffix,z_suffix+1,strlen(z_suffix+1));
  82. #endif
  83.     }
  84.     z_len = strlen(z_suffix);            
  85.  
  86.     if((currPrefs.StKeysAlt)&&(modKey))
  87.     {
  88.             Compress = true;
  89.     }
  90.     else
  91.     {
  92.         switch(currPrefs.WhenCompress)
  93.         {
  94.             case NonMacGzip:
  95.                 GetFInfo(fName,GvRefNum,&vFInfo);
  96.                 
  97.                 Compress =    (vFInfo.fdType != GZIP_ID) &&
  98.                             (vFInfo.fdType != 'ZIVU') &&
  99.                             (vFInfo.fdType != 'ZIVM') &&
  100.                             (vFInfo.fdType != 'pZIP');
  101.                             
  102.                 if (Compress) /* check suffixes */
  103.                 {
  104.                     sprintf(sTemp,"%#s",fName);
  105.                     
  106.                     c=get_suffix(sTemp);
  107.                     
  108.                     if ( c == NULL )
  109.                         Compress = true;
  110.                     else
  111.                         Compress = (*c == 0x00);
  112.                 }
  113.                 break;
  114.                 
  115.             case G_UseMenu:
  116.                 
  117.                 Compress = currPrefs.compress;
  118.                 break;
  119.         }
  120.     }
  121.     
  122.     return ( (OSErr) gzip_main ( fName, Compress ));
  123. }
  124.  
  125.  
  126. /********************************************************************************
  127.  *
  128.  * Set the correct Creator & Type (called from create_outfile)
  129.  *
  130.  * When using Fetch prefs, this should be called after
  131.  * AsciiMode
  132.  */
  133.  
  134. void FixMacFile( char* name, int ascii, int decompress)
  135. {
  136.     FInfo    vFInfo;
  137.     Str255    pname;
  138. /*    register char    *ci, *co; */
  139.             
  140.     strcpy((char*)pname,name);
  141.     CtoPstr((char*)pname);
  142.     
  143.     GetFInfo(pname,GvRefNum,&vFInfo);
  144.     
  145.  
  146.     if ( decompress)
  147.     {
  148.         if ( SufMap.Found )
  149.         {
  150.             vFInfo.fdCreator = SufMap.Creator;
  151.             vFInfo.fdType = SufMap.Type;
  152.         }
  153.         else
  154.         {
  155.             if(ascii)
  156.             {
  157.                 /* We must avoid odd address error in 68000 */
  158.                 vFInfo.fdType='TEXT';
  159.                 BlockMove((char*)currPrefs.textcreator+1,& vFInfo.fdCreator, 4);    
  160.             }
  161.             else
  162.             {
  163.                 BlockMove((char*)currPrefs.bincreator+1,& vFInfo.fdCreator, 4);
  164.                 BlockMove((char*)currPrefs.bintype+1,& vFInfo.fdType, 4);
  165.             }
  166.         }
  167.     }
  168.     else
  169.     {
  170.         vFInfo.fdType=GZIP_ID;
  171.         vFInfo.fdCreator=GZIP_ID;
  172.     }
  173.     
  174.     SetFInfo(pname,GvRefNum,&vFInfo);
  175. }
  176.  
  177. /********************************************************************************
  178.  *
  179.  *        Find the correct Creator & Type when expanding
  180.  * 
  181.  *        Set AsciiMode
  182.  */
  183.  
  184. int AsciiMode(char *name, int compress)
  185. {
  186.     
  187.     FInfo    vFInfo;
  188.     int        result, len;
  189.     
  190.     Ptr        pPrefs;
  191.     char    *p, *n, *q;
  192.  
  193.     extern char        charKey;
  194.     extern Handle    FPrefs;
  195.     extern Size        FPrefsSize;
  196.  
  197.     /*
  198.      * Mac->Net
  199.      */
  200.     if(compress) /*    name is Pascal string */
  201.     {
  202.         if((currPrefs.StKeysComp)&&(charKey!=0x00))
  203.         {
  204.             result = (charKey == 'a');
  205.         }
  206.         else
  207.         {
  208.             switch(currPrefs.AsciiCompress)
  209.             {
  210.                 case OnlyText:
  211.                     GetFInfo((StringPtr)name,GvRefNum,&vFInfo);
  212.                     result = (vFInfo.fdType == 'TEXT');
  213.                     break;
  214.                     
  215.                 case Never:
  216.                     result = 0;
  217.                     break;
  218.                     
  219.                 case UseMenu:
  220.                     result = currPrefs.ascii;
  221.                     break;
  222.                 
  223.                 case UseICinComp:
  224.                     {
  225.                         long         pos = 0;
  226.                         ICError     err;
  227.                         ICMapEntry    entry;
  228.                         int            MatchingRank, BestMatchingRank = 0;
  229.                         
  230.                         GetFInfo((StringPtr)name,GvRefNum,&vFInfo);
  231.                         result = 0;
  232.                         
  233.                         while ( noErr == ( err = ICMGetEntry(ICmappings, pos, &entry)))
  234.                         {
  235.                             /*
  236.                              *    1. type, creator and extension match    11
  237.                              *    2. type and extension match                10
  238.                              *    3. extension and creator match            7
  239.                              *    4. extension matches                    6
  240.                              *    5. type and creator match                5
  241.                              *    6. type matches                            4
  242.                              *    7. anything else
  243.                              *
  244.                              *    T=4, C=1, E=6
  245.                              */
  246.                              
  247.                             pos += entry.total_length;
  248.                             MatchingRank = 0;
  249.                             
  250.                             if (!(entry.flags & ICmap_not_outgoing_mask))
  251.                             {
  252.                                 if( vFInfo.fdType == entry.file_type )
  253.                                     MatchingRank += 4;
  254.                                     
  255.                                 if( vFInfo.fdCreator == entry.file_creator)
  256.                                     MatchingRank += 1;
  257.                                 
  258.                                 len = (int) entry.extension[0];
  259.                                 
  260.                                 if (len !=0)
  261.                                 {
  262.                                     q = (char *) &entry.extension[1];
  263.                                     
  264.                                     for( p = q, n = name + name[0] +1 - len;
  265.                                     (tolower(*p) == tolower(*n)) && (p<q+len);
  266.                                     p++, n++)
  267.                                     ;                                
  268.                                     
  269.                                     if ( n == name + name[0] +1  )
  270.                                     {
  271.                                         MatchingRank += 6;
  272.                                     }                        
  273.                                 }
  274.  
  275.                                 if ( MatchingRank > BestMatchingRank )
  276.                                 {
  277.                                     BestMatchingRank = MatchingRank;
  278.                                     result = !(entry.flags & ICmap_binary_mask);
  279.                                 }
  280.                                 
  281.                             }
  282.                         }/* while */
  283.                     }
  284.                 
  285.                     break;
  286.                     
  287.                 default:
  288.                     break;
  289.             
  290.             }
  291.         }
  292.     }
  293.     else  /* OJO *//*    name is C string */
  294.     /*
  295.      * Net -> Mac
  296.      */
  297.     {
  298.         SufMap.Found = false;
  299.         
  300.         if((currPrefs.StKeysComp)&&(charKey!=0x00))
  301.         {
  302.             result = (charKey == 'a');
  303.         }
  304.         else
  305.         {
  306.             switch(currPrefs.AsciiUnCompress)
  307.             {
  308.                 case UseICinExp:
  309.                 
  310.                     {
  311.                         long         pos = 0;
  312.                         ICError     err;
  313.                         ICMapEntry    entry;
  314.                         
  315.                         
  316.                         while ( noErr == ( err = ICMGetEntry(ICmappings, pos, &entry)))
  317.                         {
  318.                             pos += entry.total_length;
  319.                             
  320.                             if (!(entry.flags & ICmap_not_incoming_mask))
  321.                             {
  322.                                 SufMap.Type =        entry.file_type;
  323.                                 SufMap.Creator =    entry.file_creator;
  324.                                 SufMap.Binary =        (entry.flags & ICmap_binary_mask);
  325.                                 SufMap.BinHex =        0;
  326.                                 /*(entry.flags & ICmap_resource_fork_mask)*/
  327.                                 
  328.                                 len = (int) entry.extension[0];
  329.                                 
  330.                                 if (len !=0)
  331.                                 {
  332.                                     q = (char *) &entry.extension[1];
  333.                                     
  334.                                     for( p = q, n = name + strlen(name) - len;
  335.                                     (tolower(*p) == tolower(*n)) && (p<q+len);
  336.                                     p++, n++)
  337.                                     ;                                
  338.                                     
  339.                                     if ( n == name + strlen(name) )
  340.                                     {
  341.                                         SufMap.Found = true;
  342.                                         break;
  343.                                     }                        
  344.                                 }
  345.                             }
  346.                         }
  347.  
  348.                         if(SufMap.Found)
  349.                         {
  350.                             result = !SufMap.Binary;
  351.                         }
  352.                         else
  353.                         {
  354.                             result = 0;
  355.                         }
  356.  
  357.                     }
  358.                     break;
  359.                     
  360.  
  361.                     
  362.                 case Fetch:
  363.                 
  364.                     HLock( FPrefs );
  365.                     pPrefs = *FPrefs;
  366.                     
  367.                     
  368.  
  369.                     while( pPrefs < *FPrefs+FPrefsSize )
  370.                     {
  371.                         SufMap.Type =        *(OSType *)pPrefs;            pPrefs += 4;
  372.                         SufMap.Creator =    *(OSType *)pPrefs;            pPrefs += 4;
  373.                         SufMap.Binary =        (*(short *)pPrefs != 0);    pPrefs += 2;
  374.                         SufMap.BinHex =        (*(short *)pPrefs != 0);    pPrefs += 2;
  375.                         
  376.                         p = SufMap.Suffix;
  377.                         while((*p++ = *(char *)(pPrefs++))!=0)
  378.                         ;
  379.                         
  380. #ifdef    GET_DESC
  381.                         p = SufMap.Description;
  382.                         while((*p++= *(char *)(pPrefs++))!=0)
  383.                         ;
  384. #else
  385.                         while(*(char *)(pPrefs++)!=0)
  386.                         ;
  387. #endif
  388.                         len = strlen(SufMap.Suffix);
  389.                         
  390.                         for( p = SufMap.Suffix, n = name + strlen(name) - len;
  391.                         (tolower(*p) == tolower(*n)) && (p<SufMap.Suffix+len);
  392.                         p++, n++)
  393.                         ;
  394.                     
  395.                         if ( n == name + strlen(name) )
  396.                         {
  397.                             SufMap.Found = true;
  398.                             break;
  399.                         }                        
  400.                     }
  401.  
  402.                     HUnlock( FPrefs );
  403.  
  404.                     if(SufMap.Found)
  405.                     {
  406.                         result = !SufMap.Binary;
  407.                     }
  408.                     else
  409.                     {
  410.                         result = 0;
  411.                     }
  412.                     
  413.                     break;
  414.  
  415.                 case Never:
  416.                     result = 0;
  417.                     break;
  418.                     
  419.                 case UseMenu:
  420.                     result = currPrefs.ascii;
  421.                     break;
  422.                     
  423.                 default:
  424.                     break;
  425.             }
  426.         }
  427.     }
  428.     
  429.     return (result);
  430. }
  431.